package com.hdl.servlet.order;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hdl.dto.OrderSearch;
import com.hdl.entity.MallOrder;
import com.hdl.service.MallOrderService;
import com.hdl.service.impl.MallOrderServiceImpl;
import com.hdl.vo.PageVO;

@WebServlet("/admin-order-page.let")
public class OrderPageServlet extends HttpServlet{

	Logger log = LoggerFactory.getLogger(this.getClass());
	private OrderSearch search = new OrderSearch();
	private MallOrderService service = new MallOrderServiceImpl();
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		Integer current = 1;
		Integer rowMax = 10;
		if(req.getParameter("key")!=null) {
			search.setKey(req.getParameter("key"));
		}
		if(req.getParameter("current")!=null) {
			current = Integer.valueOf(req.getParameter("current"));
		}
		if(req.getParameter("pageSize")!=null) {
			rowMax = Integer.valueOf(req.getParameter("pageSize"));
		}
		PageVO<MallOrder> page = service.selectSearch(search, current, rowMax);
		log.debug(JSON.toJSONString(page));
		req.setAttribute("page", page);
		ObjectMapper mapper = new ObjectMapper();
		resp.setContentType("application/json;charset=UTF-8");
		mapper.writeValue(resp.getOutputStream(), page);
		//req.getRequestDispatcher("admin/order/order-list.jsp").forward(req, resp);
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	
}
